﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;//added for forms authentication

namespace ShoppingApp
{
    public partial class MyLogin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        //Login button
        protected void Button1_Click(object sender, EventArgs e)
        {
            //FormsAuthentication.Initialize();

            //Fill user data
            string userName = "Michal";
            string userRole = "Admin";

            //Create ticket
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, userName, DateTime.Now,
                DateTime.Now.AddMinutes(1), false, userRole, FormsAuthentication.FormsCookiePath);

            //Encrypt ticket
            string encryptedTicket = FormsAuthentication.Encrypt(ticket);
            
            //Create cookie
            HttpCookie aCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

            //Send cookie to the browser
            Response.Cookies.Add(aCookie);

            //Redirect user to originated page
            string url = Request.QueryString["ReturnUrl"];
            if (url != null)
            {
                Response.Redirect(url);
            }
            
            //Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName,
            //    FormsAuthentication.Encrypt(fat)));
            //Response.Redirect(FormsAuthentication.GetRedirectUrl("Michal", false));
        }
    }
}